#include <iostream>
#include <map>
#include <string>
using namespace std;
// TODO:

int n, m;
map< string, int > tab;
int e_cnt, heads[51], ans = 1;
struct Edge {
	int v, nxt;
} e[5000 + 5];
inline void add(int u, int v) {
	e[++e_cnt].v = v;
	e[e_cnt].nxt = heads[u];
	heads[u] = e_cnt;
}
bool vis[51];
void dfs(int x, int fa, int sum) {
	if (x == 1 && sum != 1) {
		ans = max(ans, sum);
		return;
	}
	for (int i = heads[x]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (vis[v] || v == fa) continue;
		vis[v] = true;
		dfs(v, x, sum + 1);
		vis[v] = false;
	}
}
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("tour.in", "r", stdin);
	freopen("tour.out", "w", stdout);
#endif
#endif
	cin >> n >> m;
	string tmp, tmp2;
	for (int i = 1; i <= n; ++i) {
		cin >> tmp;
		tab[tmp] = i;
	}
	for (int i = 1; i <= m; ++i) {
		cin >> tmp >> tmp2;
		if (tmp == tmp2) continue;
		add(tab[tmp], tab[tmp2]);
		add(tab[tmp2], tab[tmp]);
	}
	dfs(1, 0, 1);
	printf("%d", ans == 1 ? 1 : ans - 1);
	return 0;
}